package me.luger.project.picc.service.picc;

import me.luger.core.commom.util.DateUtil;
import me.luger.core.dao.cache.redis.springdata.RedisManager;
import me.luger.project.picc.entity.ActivityEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by a on 2017/5/4.
 */
@Service
public class TongjiService {

    @Autowired
    RedisManager redisManager;

    /**
     * 活动参与人数
     * @param openId
     */
    public void addUserCount(String openId, ActivityEnum activityEnum,int typeId){
        String nowDateFmt = DateUtil.formatNow(DateUtil.DATE_YYYYMMDD);

        if(null != activityEnum){
            typeId = activityEnum.getActivityId();
        }
//        if(!redisManager.sismember("allUserCount",openId)){
            redisManager.sadd("userCount:"+nowDateFmt,openId);//所有活动的日新增
            redisManager.sadd("activity:userCount:"+typeId+":"+nowDateFmt,openId);//某一个活动的日新增
//        }

        redisManager.sadd("allUserCount",openId);
        redisManager.sadd("activity:allUserCount:"+typeId,openId);

        redisManager.incr("timesCount:"+nowDateFmt);//所有活动的日新增
        redisManager.incr("activity:timesCount:"+typeId+":"+nowDateFmt);//某一个活动的日新增

        redisManager.incr("allTimesCount");//所有活动的次数统计
        redisManager.incr("activity:allTimesCount:"+typeId);
    }

    public Long getAllUserCount(){
        Long allUserCount = redisManager.scard("allUserCount");
        return allUserCount;
    }

    public Long getUserCountByDate(Date date){
        String dateFmt = DateUtil.format(date,DateUtil.DATE_YYYYMMDD);
        return redisManager.scard("userCount:"+dateFmt);
    }

    public Long getActivityAllUserCount(int typeId){
        return redisManager.scard("activity:allUserCount:"+typeId);
    }

    public Long getActivityUserCountByDate(int typeId,Date date){
        String dateFmt = DateUtil.format(date,DateUtil.DATE_YYYYMMDD);
        return redisManager.scard("activity:userCount:"+typeId+":"+dateFmt);
    }

//    /**
//     * 活动参与人次
//     */
//    public void addTimesCount(ActivityEnum activityEnum){
//        String nowDateFmt = DateUtil.formatNow(DateUtil.DATE_YYYYMMDD);
//        redisManager.incr("timesCount:"+nowDateFmt);//所有活动的日新增
//        redisManager.incr("activity:timesCount:"+activityEnum.getActivityId()+":"+nowDateFmt);//某一个活动的日新增
//
//        redisManager.incr("allTimesCount");//所有活动的次数统计
//        redisManager.incr("activity:allTimesCount:"+activityEnum.getActivityId());
//    }

    public Long getAllTimesCount(){
        String allTimesCount = redisManager.get("allTimesCount");
        if(null == allTimesCount){
            return 0L;
        }
        return Long.valueOf(allTimesCount);
    }

    public Long getTimesCountByDate(Date date){
        String dateFmt = DateUtil.format(date,DateUtil.DATE_YYYYMMDD);
        String timesCount = redisManager.get("timesCount:"+dateFmt);
        if(null == timesCount){
            return 0L;
        }
        return Long.valueOf(timesCount);
    }

    public Long getActivityAllTimesCount(int typeId){
        String activityTimesCount = redisManager.get("activity:allTimesCount:"+typeId);
        if(null == activityTimesCount){
            return 0L;
        }
        return Long.valueOf(activityTimesCount);
    }

    public Long getActivityTimesCountByDate(int typeId,Date date){
        String dateFmt = DateUtil.format(date,DateUtil.DATE_YYYYMMDD);
        String activityTimesCount = redisManager.get("activity:timesCount:"+typeId+":"+dateFmt);
        if(null == activityTimesCount){
            return 0L;
        }
        return Long.valueOf(activityTimesCount);
    }



//    /**
//     * 中奖人数
//     * @param openID
//     * @param pid
//     */
//    public void addGetPrizeUserCount(String openID,String pid, ActivityEnum activityEnum){
//        String nowDateFmt = DateUtil.formatNow(DateUtil.DATE_YYYYMMDD);
//
//        redisManager.sadd("allGetPrizeUserCount:openId:pid",openID+"-"+pid);
//        redisManager.sadd("allGetPrizeUserCount:openId",openID);
//        redisManager.sadd("allGetPrizeUserCount:pid",pid);
//
//        redisManager.sadd("getPirzeUserCount:"+nowDateFmt,openID);
//        redisManager.sadd("activity:getPirzeUserCount:"+activityEnum.getActivityId()+":"+nowDateFmt,openID);
//    }




    public void addCustom(String openId){
        String dateFmt = DateUtil.formatNow(DateUtil.DATE_YYYYMMDD);
        redisManager.sadd("custom:uv:all",openId);//总参与人数
        redisManager.sadd("custom:uv:"+dateFmt,openId);//当日参与人数

        redisManager.incr("custom:pv:all");//总参与次数
        redisManager.incr("custom:pv:"+dateFmt);//当日参与次数
    }



}

